package com.czollmann.android.views;

import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.HorizontalScrollView;

public class CustomScrollView extends HorizontalScrollView {
  private GestureDetector gestureDetector;

  public CustomScrollView(Context context) {
    super(context);
    gestureDetector = new GestureDetector(new YScrollDetector());
    setFadingEdgeLength(0);
  }

  @Override
  public boolean onTouchEvent(MotionEvent ev) {
    return super.onTouchEvent(ev);
  }

  @Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {
    // Call super first because it does some hidden motion event handling
    boolean result = super.onInterceptTouchEvent(ev);
    // Now see if we are scrolling vertically with the custom gesture detector
    if (gestureDetector.onTouchEvent(ev)) {
      return result;
    }
    // If not scrolling vertically (more y than x), don't hijack the event.
    else {
      return false;
    }
  }

  // Return false if we're scrolling in the x direction
  class YScrollDetector extends SimpleOnGestureListener {
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
      try {
        if (Math.abs(distanceX) > Math.abs(distanceY)) {
          return true;
        } else {
          return false;
        }
      }
      catch (Exception e) {
        // nothing
      }
      return false;
    }
  }
}
